<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="queryForm" class="demo-form-inline" ref="queryForm">
          <el-form-item label="资源名称" prop="name">
            <el-input v-model="queryForm.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="queryForm.url" placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="categoryId">
            <el-select v-model="queryForm.categoryId" placeholder="请选择资源分类" clearable>
              <el-option :label="category.name" :value="category.id" v-for="category in categories" :key="category.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="" @click="reset" :disabled="isLoading">重置</el-button>
            <el-button type="info" @click="search" :disabled="isLoading">查询搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div slot="header" class="clearfix">
        <div>
          <el-button size="mini" @click="onSubmit">添加</el-button>
          <el-button size="mini" @click="onSubmit">资源分类</el-button>
        </div>
      </div>
      <el-table :data="resources" style="width: 100%" border v-loading="isLoading">
        <el-table-column label="编号" type="index" width="50">
        </el-table-column>
        <el-table-column prop="name" label="资源名称">
        </el-table-column>
        <el-table-column prop="url" label="资源路径">
        </el-table-column>
        <el-table-column prop="description" label="描述">
        </el-table-column>
        <el-table-column prop="createdTime" label="添加时间" :formatter="formatDate">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="queryForm.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryForm.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="padding: 10px;"
      :disabled="isLoading">
    </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getResourcePages } from '@/services/resource'
import { getResourceCategories } from '@/services/resource-category'
import { Form } from 'element-ui'
import { formatDate } from '@/utils/formatDate'

export default Vue.extend({
  name: 'ResourceList',
  data () {
    return {
      queryForm: {
        name: '',
        url: '',
        categoryId: null,
        current: 1,
        size: 10
      },
      total: 0,
      resources: [],
      currentPage4: 4,
      categories: [],
      isLoading: true
    }
  },
  created () {
    this.loadResource()
    this.loadResourceCategories()
  },
  methods: {
    handleSizeChange (val: number) {
      this.queryForm.size = val
      this.queryForm.current = 1
      this.loadResource()
    },
    handleCurrentChange (val: number) {
      this.queryForm.current = val
      this.loadResource()
    },
    onSubmit () {
      console.log('submit')
    },
    search () {
      this.queryForm.current = 1
      this.loadResource()
    },
    reset () {
      (this.$refs.queryForm as Form).resetFields()
      this.queryForm.current = 1
      this.loadResource()
    },
    handleEdit (item: any) {
      console.log(item)
    },
    handleDelete (item: any) {
      console.log(item)
    },
    async loadResource () {
      this.isLoading = true
      const { data } = await getResourcePages(this.queryForm)
      this.resources = data.data.records
      this.queryForm.current = data.data.current
      this.total = data.data.total
      this.isLoading = false
    },
    async loadResourceCategories () {
      const { data } = await getResourceCategories()
      this.categories = data.data
    },
    formatDate
  }
})
</script>

<style lang="scss" scope></style>
